package cn.stylefeng.guns.modular.business.frequency.orderbonus.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.context.login.LoginContextHolder;
import cn.stylefeng.guns.core.exception.enums.StatusExceptionEnum;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.AndaPageResult;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.core.pojo.response.ResponseData;
import cn.stylefeng.guns.modular.business.custom.enums.BonusTypeEnum;
import cn.stylefeng.guns.modular.business.frequency.orderbonus.entity.OrderBonus;
import cn.stylefeng.guns.modular.business.frequency.orderbonus.mapper.OrderBonusMapper;
import cn.stylefeng.guns.modular.business.frequency.orderbonus.model.param.OrderBonusParam;
import cn.stylefeng.guns.modular.business.frequency.orderbonus.model.result.OrderBonusDetail;
import cn.stylefeng.guns.modular.business.frequency.orderbonus.model.result.OrderBonusStatistics;
import cn.stylefeng.guns.modular.business.frequency.orderbonus.model.result.OrderbonusSum;
import cn.stylefeng.guns.modular.business.frequency.orderbonus.service.OrderBonusService;
import cn.stylefeng.guns.modular.business.frequency.orderbonus.enums.OrderBonusExceptionEnum;
import cn.stylefeng.guns.modular.business.frequency.syssite.entity.SysSite;
import cn.stylefeng.guns.modular.business.frequency.syssite.service.SysSiteService;
import cn.stylefeng.guns.modular.business.util.BusinessUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 奖金记录表(activityMq异步处理) 服务实现类
 *
 * @author yujie
 * @date 2023/05/09 17:16
 */
@Service
public class OrderBonusServiceImpl extends ServiceImpl<OrderBonusMapper, OrderBonus> implements OrderBonusService {

    @Autowired
    private SysSiteService sysSiteService;
    @Autowired
    private BusinessUtil businessUtil;

    @Override
    public PageResult<OrderBonus> page(OrderBonusParam orderBonusParam) {

        // 构造条件
        LambdaQueryWrapper<OrderBonus> queryWrapper = new LambdaQueryWrapper<>();


        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<OrderBonus> list(OrderBonusParam orderBonusParam) {

        // 构造条件
        LambdaQueryWrapper<OrderBonus> queryWrapper = new LambdaQueryWrapper<>();


        return this.list(queryWrapper);
    }

    @Override
    public void add(OrderBonusParam orderBonusParam) {

        // 将dto转为实体
        OrderBonus orderBonus = new OrderBonus();
        BeanUtil.copyProperties(orderBonusParam, orderBonus);


        this.save(orderBonus);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(OrderBonusParam orderBonusParam) {

        // 根据id查询实体
        OrderBonus orderBonus = this.queryOrderBonus(orderBonusParam);


        // 更新实体
        this.updateById(orderBonus);

    }

    @Override
    public void edit(OrderBonusParam orderBonusParam) {

        // 根据id查询实体
        OrderBonus orderBonus = this.queryOrderBonus(orderBonusParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(orderBonusParam, orderBonus);

        // 不能修改状态，用修改状态接口修改状态
        orderBonus.setStatus(null);

        this.updateById(orderBonus);
    }

    @Override
    public OrderBonus detail(OrderBonusParam orderBonusParam) {
        return this.queryOrderBonus(orderBonusParam);
    }

    @Override
    public void changeStatus(OrderBonusParam orderBonusParam) {
        String id = orderBonusParam.getId();
        Integer status = orderBonusParam.getStatus();


        // 更新枚举，更新只能更新未删除状态的
        LambdaUpdateWrapper<OrderBonus> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
            .eq(OrderBonus::getId, id)
                .set(OrderBonus::getStatus, status);
        boolean update = this.update(updateWrapper);
        if (!update) {
            throw new ServiceException(StatusExceptionEnum.UPDATE_STATUS_ERROR);
        }
    }

    @Override
    public ResponseData appPage(OrderBonusParam orderBonusParam) {
        Page<OrderbonusSum> page = this.baseMapper.getAppPage(PageFactory.defaultPage(),orderBonusParam, LoginContextHolder.me().getSysLoginUserId());
        SysSite sysSite = sysSiteService.getById(businessUtil.getSiteId());
        for(OrderbonusSum orderbonusSum : page.getRecords()){
            orderbonusSum.setSite(sysSite.getName());
        }
        BigDecimal feeSum = this.baseMapper.getSiteSum(LoginContextHolder.me().getSysLoginUserId());
        Map<String,Object> map = new HashMap<>();
        map.put("date",new PageResult<>(page));
        map.put("sumFee",feeSum);
        return ResponseData.success(map);
    }

    @Override
    public ResponseData appDetail(OrderBonusParam orderBonusParam) {
        Page<OrderBonusDetail> page =  this.baseMapper.getAppDetail(PageFactory.defaultPage(),orderBonusParam,LoginContextHolder.me().getSysLoginUserId());
        for (OrderBonusDetail detail : page.getRecords()){
            detail.setTitle(BonusTypeEnum.valid(detail.getType()));
        }
        return ResponseData.success(new PageResult<>(page));
    }

    @Override
    public ResponseData statistics(OrderBonusParam orderBonusParam) {
        OrderBonusStatistics orderBonus = this.baseMapper.statistics(orderBonusParam,LoginContextHolder.me().getSysLoginUserId());
        return ResponseData.success(orderBonus);
    }


    /**
     * 获取奖金记录表(activityMq异步处理)
     *
     * @author yujie
     * @since 2023/05/09 17:16
     */
    private OrderBonus queryOrderBonus(OrderBonusParam orderBonusParam) {
        OrderBonus orderBonus = this.getById(orderBonusParam.getId());
        if (ObjectUtil.isEmpty(orderBonus)) {
            throw new ServiceException(OrderBonusExceptionEnum.NOT_EXISTED);
        }
        return orderBonus;
    }
}